import {
	getBookShelfList,
	addBookToShelf,
	delBookFromShelf
} from '@/api/shelf'
const shelfModules = {
	state: {
		shelfList: []
	},
	getters: {},
	mutations: {
		SET_BOOK_SHELF_LIST(state,shelfList){
			state.shelfList = shelfList
		}
	},
	actions: {
		// 更新书架
		updateShelfList({
			rootState,
			commit
		}) {
			return new Promise((resolve, reject) => {
				let header = {
					Authorization: 'Bearer ' + rootState.global.token
				}
				getBookShelfList(header).then(res => {
					commit('SET_BOOK_SHELF_LIST',res.data.items)
					resolve(res.data.items)
				}).catch(e =>{
					reject(e)
				})
			})
		},
		// 新增书籍
		addBookToShelf({ rootState }, {bookId, lastReadChapter, index}){
			return new Promise((resolve, reject) =>{
				addBookToShelf({
					Authorization: 'Bearer ' + rootState.global.token
				}, {
					bookId,
					lastReadChapter,
					index
				}).then(res => {
					resolve('ok')
				}).catch(e =>{
					reject(e)
				})
			})
		},
		// 移除书籍
		delBookFromShelf({rootState}, bookShelfId){
			return new Promise((resolve,rejcet) =>{
				delBookFromShelf({
					Authorization: 'Bearer ' + rootState.global.token
				}, {
					bookShelfId
				}).then(res => {
					resolve('ok')
				}).catch(e =>{
					reject(e)
				})
			})
		}
	}
}

export default shelfModules
